
$(function () {

    //获取权限
    console.log("获取权限")
    $.ajax({
        url:"login",
        dataType:"JSON",
        type:"get",
        success:function (data) {

            console.log(data)
            if (data.extend.role=="3"){
                return
            }else{
                $(".shows").css({display:"none"})
            }
        },
        error:function (err) {
            console.log(err)
        }
    })

    $(".lodding").css({display:"block"})
    function getFormatDate(){
        var nowDate = new Date(new Date().getTime() - 86400000);
        var year = nowDate.getFullYear();
        var month = nowDate.getMonth() + 1 < 10 ? "0" + (nowDate.getMonth() + 1) : nowDate.getMonth() + 1;
        var date = nowDate.getDate() < 10 ? "0" + nowDate.getDate() : nowDate.getDate()
        return year + "-" + month + "-" + date;
    }
    var str = getFormatDate();
    $(".times").val(str)
    fun(str)
})

console.log("长度："+$(".times").length)

for (var i = 0; i<$(".times").length;i++){
    $(".times").eq(i).change(function () {
        var index = $(".times").index($(this))
        console.log(index)
        if (index==0){
            fun0($(".times").eq(index).val())
        }else if(index==1){
            fun1($(".times").eq(index).val())
        }else if (index == 2){
            fun2($(".times").eq(index).val())
        }
    })

}

//数组去重
function quchong(array){
    var temp = []; //一个新的临时数组
    for(var i = 0; i < array.length; i++){
        if(temp.indexOf(array[i]) == -1){
            temp.push(array[i]);
        }
    }
    return temp;
}

function fun(times) {
    fun0(times)
    fun1(times)
    fun2(times)


}

function fun0(times) {
    //教学楼上课次数
    $.ajax({
        url:"getemNames",
        dataType: "JSON",
        type:"GET",
        data:{times:times},  //当前时间
        success:function (data) {
            console.log("教学楼上课次数")
            console.log(data)

            $(".lodding").css({display:"none"})
            var data1 = []
            if (data.extend.rmName.length==0){
                $("#jiaoxue").css({display:"none"})
                $(".none_msg").eq(0).css({display:"block"})
            }else{
                $("#jiaoxue").css({display:"block"})
                $(".none_msg").eq(0).css({display:"none"})
                for (var i=0;i<data.extend.rmName.length;i++){
                    //上课楼号
                    var str = (data.extend.rmName[i].rmname).substring(0,data.extend.rmName[i].rmname.length-4)
                    if(str=="2"){
                        data1.push(str+"号组团楼")
                    }else{
                        data1.push(str+"号楼");
                    }

                }
                var arr1 = quchong(data1)   //上课楼号
                //统计个数
                var counts = {};
                jQuery.each(data1,function (key,value){
                    if(!counts.hasOwnProperty(value)){
                        counts [value] = 1;
                    } else {
                        counts [value] ++ ;
                    }
                });
                var arr2 = []
                for (var i=0;i<arr1.length;i++){
                    arr2.push(counts[arr1[i]])
                }

                if (arr2.length==0){
                    arr1=["1号楼","2号组团楼","3号楼","4号楼","11号楼"]
                    arr2=[0,0,0,0,0]
                }

                //教学楼上课次数*
                var jiaoxue = echarts.init(document.getElementById("jiaoxue"))
                option1 = {
                    color: ['#3398DB'],
                    title: {
                        text: '教学楼上课次数',
                        subtext: times,
                        left: 'center'
                    },
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                            type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                        }
                    },
                    grid: {
                        left: '3%',
                        right: '4%',
                        bottom: '3%',
                        containLabel: true
                    },
                    xAxis: [
                        {
                            type: 'category',
                            data: arr1,
                            axisTick: {
                                alignWithLabel: true
                            }
                        }
                    ],
                    yAxis: [
                        {
                            type: 'value'
                        }
                    ],
                    series: [
                        {
                            name: '上课次数',
                            type: 'bar',
                            barWidth: '60%',
                            data: arr2
                        }
                    ]
                };
                jiaoxue.setOption(option1)
            }

        }
    })

}
function fun1(times) {
    // 上课方式
    $.ajax({
        url:'countfangshi',
        dataType:"JSON",
        type:"GET",
        data:{times:times},
        success:function (data) {
            console.log("上课方式")
            console.log(data)
            $(".lodding").css({display:"none"})
            if (data.extend.count1==0&&data.extend.count2==0){
                $("#main").css({display:"none"})
                $(".none_msg").eq(1).css({display:"block"})
            }else{
                $("#main").css({display:"block"})
                $(".none_msg").eq(1).css({display:"none"})
                var shangke = echarts.init(document.getElementById('main'));
                option2 = {
                    title: {
                        text: '上课方式对比',
                        subtext: times,
                        left: 'center'
                    },
                    tooltip: {
                        trigger: 'item',
                        formatter: '{a} <br/>{b} : {c} ({d}%)'
                    },
                    legend: {
                        orient: 'vertical',
                        left: 'left',
                        data: ['刷卡', '扫码']
                    },
                    series: [
                        {
                            name: '操作次数',
                            type: 'pie',
                            radius: '55%',
                            center: ['50%', '60%'],
                            data: [
                                {value: data.extend.count1, name: '刷卡'},
                                {value: data.extend.count2, name: '扫码'},

                            ],
                            emphasis: {
                                itemStyle: {
                                    shadowBlur: 10,
                                    shadowOffsetX: 0,
                                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                                }
                            }
                        }
                    ]
                };
                shangke.setOption(option2);
            }
        }
    })

}

function fun2(times) {

    //学生上课考勤统计
    $.ajax({
        url:"countType",
        dataType:"JSON",
        type:"GET",
        data:{imgtime:"2021-04-13"},
        success:function (data) {
            console.log("sdadasdasda")
            console.log(data)
            if (data.extend.count1==0&&data.extend.count2==0){
                $("#shijian").css({display:"none"})
                $(".none_msg").eq(2).css({display:"block !important"})
            }else{

                $("#shijian").css({display:"block"})
                $(".none_msg").eq(2).css({display:"none"})
                var shijian = echarts.init(document.getElementById('shijian'));
                option2 = {
                    title: {
                        text: '学生上课情况',
                        subtext: times,
                        left: 'center'
                    },
                    tooltip: {
                        trigger: 'item',
                        formatter: '{a} <br/>{b} : {c} ({d}%)'
                    },
                    legend: {
                        orient: 'vertical',
                        left: 'left',
                        data: ['总人数', '上课人数','睡觉人数']
                    },
                    series: [
                        {
                            name: '操作次数',
                            type: 'pie',
                            radius: '55%',
                            center: ['50%', '60%'],
                            data: [
                                {value: data.extend.count1, name: '总人数'},
                                {value: data.extend.count2, name: '上课人数'},
                                {value: data.extend.count3, name: '睡觉人数'}

                            ],
                            emphasis: {
                                itemStyle: {
                                    shadowBlur: 10,
                                    shadowOffsetX: 0,
                                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                                }
                            }
                        }
                    ]
                };
                shijian.setOption(option2);
            }

        },
        error:function (err) {
            // console.log("获取失败")
            console.log(err)
        }
    })

}



